package com.customization.shunsheng.util;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LeaveNumberUtil {
    BaseBean basebean = new BaseBean();

    /**
     * 清理外籍人员的假期余额
     * @param ruleId 假期类型id
     */
    public void clearForgResLeaveNumber(String ruleId) {
        RecordSet rs = new RecordSet();

        Integer isForg = -1;
        Integer corresLeaveType = -1;
        List<String> resList = new ArrayList<>();
        String today = DateUtil.format(new Date(),"yyyy-MM-dd");
        basebean.writeLog("today: "+today);

        //根据ruleId判断该假期类型是否为外籍类型
        String checkForSql = "select isForg,corresLeaveType from kq_LeaveRules where id = ?";
        rs.executeQuery(checkForSql,ruleId);
        while (rs.next()) {
            isForg = Util.getIntValue(Util.null2String(rs.getString("isForg")));
            corresLeaveType = Util.getIntValue(Util.null2String(rs.getString("corresLeaveType")));
        }
        basebean.writeLog("corresLeaveType: "+corresLeaveType);
        if (isForg == 1) {//外籍
            String getResSql = "select ygxm from uf_jqlxb where jqlx = ? and isdelete = 1";
            rs.executeQuery(getResSql,ruleId);
            while (rs.next()) {
                String ygxm = Util.null2String(rs.getString("ygxm"));
                if (StringUtils.isNotBlank(ygxm)) {
                    resList.add(ygxm);
                }
            }

            if ( !resList.isEmpty()) {
                //去对应的假期类型中将非对应的外籍人员数据清空
//                    baseAmount -- 假期余额基数
//                    usedAmount -- 已用假期余额
//                    extraAmount -- 额外假期余额
//                    baseAmount2 -- 假期余额基数
//                    usedAmount2 -- 已用假期余额
//                    extraAmount2 -- 额外假期余额
                String resIds = String.join(",", resList);
                String removeNfSql ="update kq_balanceOfLeave set baseAmount = 0.00, usedAmount = 0.00, extraAmount = 0.00, " +
                        "baseAmount2 = 0.00, usedAmount2 = 0.00, extraAmount2 = 0.00, expirationDate = ? where leaveRulesId = ? and resourceId not in ("+resIds+")";
                rs.executeUpdate(removeNfSql,today,ruleId);
                //同时将这些外籍人员在对应非外籍假期类型里面的假期基数清空
                String removefSql ="update kq_balanceOfLeave set baseAmount = 0.00, usedAmount = 0.00, extraAmount = 0.00, " +
                        "baseAmount2 = 0.00, usedAmount2 = 0.00, extraAmount2 = 0.00, expirationDate = ? where leaveRulesId = ? and resourceId in ("+resIds+")";
                basebean.writeLog("removefSql: "+removefSql);
                rs.executeUpdate(removefSql,today,corresLeaveType);

            }
        } else {
            if (corresLeaveType > -1 ) {//批处理时如果是非外籍假期类型，但是有外籍假期类型对应，将该假期类型中的外籍假期类型的人员假期清空
                String getResSql = "select ygxm from uf_jqlxb where jqlx = ? and isdelete = 1";
                rs.executeQuery(getResSql,corresLeaveType);
                while (rs.next()) {
                    String ygxm = Util.null2String(rs.getString("ygxm"));
                    if (StringUtils.isNotBlank(ygxm)) {
                        resList.add(ygxm);
                    }
                }

                if ( !resList.isEmpty()) {

                    String resIds = String.join(",", resList);

                    String removefSql ="update kq_balanceOfLeave set baseAmount = 0.00, usedAmount = 0.00, extraAmount = 0.00, " +
                            "baseAmount2 = 0.00, usedAmount2 = 0.00, extraAmount2 = 0.00, expirationDate = ? where leaveRulesId = ? and resourceId in ("+resIds+")";
                    basebean.writeLog("removefSql: "+removefSql);
                    rs.executeUpdate(removefSql,today,ruleId);

                }
            }
        }

    }

}
